//
//  DetailUpDownPriceCallOutView.m
//  PriceOnline
//
//  Created by Le Anh Tai on 8/29/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "DetailUpDownPriceCallOutView.h"
#import "UIConstants.h"
#import "Utility.h"
#import "GoldPriceViewController.h"
#import "Global.h"
#import "RootViewControler.h"

@implementation DetailUpDownPriceCallOutView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.arrowPos = ArrowPos_BottomCenter;
        
        UIColor *textColor = [UIColor blackColor];
        
        _lastUpdateDateLabel = [self createLabelWithTextCorlor:textColor textAlignment:NSTextAlignmentRight];
        _lastUpdateBuyLabel = [self createLabelWithTextCorlor:textColor textAlignment:NSTextAlignmentRight];
        _lastUpdateSellLabel = [self createLabelWithTextCorlor:textColor textAlignment:NSTextAlignmentRight];
        _lastUpdateDateLabel.textColor = [UIColor grayColor];
        _lastUpdateBuyLabel.textColor = [UIColor grayColor];
        _lastUpdateSellLabel.textColor = [UIColor grayColor];
        
        _currentUpdateDateLabel = [self createLabelWithTextCorlor:textColor textAlignment:NSTextAlignmentRight];
        _currentUpdateBuyLabel = [self createLabelWithTextCorlor:textColor textAlignment:NSTextAlignmentRight];
        _currentUpdateSellLabel = [self createLabelWithTextCorlor:textColor textAlignment:NSTextAlignmentRight];
        
        _lineView = [[LineView alloc]init];
        [self addSubview:_lineView];
        
        _changeLabel = [self createLabelWithTextCorlor:[UIColor blueColor] textAlignment:NSTextAlignmentRight];
        _buyChangeLabel = [self createLabelWithTextCorlor:textColor textAlignment:NSTextAlignmentRight];
        _sellChangeLabel = [self createLabelWithTextCorlor:textColor textAlignment:NSTextAlignmentRight];
        
        _buyChangeLabel.numberOfLines = _sellChangeLabel.numberOfLines = 2;
        
        self.arrowPos = ArrowPos_BottomCenter;
    }
    
    return self;
}

- (UILabel*)createLabelWithTextCorlor:(UIColor*)textColor textAlignment:(NSTextAlignment)textAlignment
{
    UILabel *textLabel = [[UILabel alloc]init];
    textLabel.backgroundColor = [UIColor clearColor];
    textLabel.textAlignment = textAlignment;
    textLabel.textColor = textColor;
    textLabel.font = FONT_HELVETICANEUE(13);
    [self addSubview:textLabel];
    return textLabel;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    NSInteger heightLabel = 20;
    NSInteger paddingRight = 20;
    NSInteger paddingTop = 10;
    NSInteger typeWid = 3*(self.width - paddingRight)/5;
    NSInteger priceWid = (self.width - paddingRight)/5;
    
    _lastUpdateDateLabel.frame = RECT(0, self.padding.top + paddingTop, typeWid, heightLabel);
    _lastUpdateBuyLabel.frame = RECT(_lastUpdateDateLabel.right, _lastUpdateDateLabel.top, priceWid, heightLabel);
    _lastUpdateSellLabel.frame = RECT(_lastUpdateBuyLabel.right, _lastUpdateDateLabel.top, self.width - _lastUpdateBuyLabel.right - paddingRight, heightLabel);
    
    _currentUpdateDateLabel.frame = _lastUpdateDateLabel.frame;
    _currentUpdateDateLabel.top = _lastUpdateDateLabel.bottom;
    _currentUpdateBuyLabel.frame = _lastUpdateBuyLabel.frame;
    _currentUpdateBuyLabel.top = _lastUpdateBuyLabel.bottom;
    _currentUpdateSellLabel.frame = _lastUpdateSellLabel.frame;
    _currentUpdateSellLabel.top = _lastUpdateSellLabel.bottom;
    
    _lineView.frame = RECT(0, _currentUpdateDateLabel.bottom + paddingTop/2,
                           self.width - paddingRight*2, 1);
    _lineView.centerX = self.width/2;
    
    _changeLabel.frame = RECT(0, _lineView.bottom + paddingTop/2,
                              _currentUpdateDateLabel.width, self.height - self.padding.bottom - _lineView.bottom - paddingTop);
    _buyChangeLabel.frame = RECT(_currentUpdateBuyLabel.left, _changeLabel.top, _currentUpdateBuyLabel.width, _changeLabel.height);
    _sellChangeLabel.frame = RECT(_currentUpdateSellLabel.left, _changeLabel.top, _currentUpdateSellLabel.width, _changeLabel.height);
    
    [self setNeedsDisplay];
    [_lineView setNeedsDisplay];
}

- (void)loadDataWithGoldPriceDictionary:(NSDictionary *)dict lastDate:(NSString *)lastDate currentDate:(NSString *)currentDate
{
    UIColor *changeTextColor = [UIColor blackColor];
    
    //
    if (lastDate.length > 0)
    {
        _lastUpdateDateLabel.text = [NSString stringWithFormat:TEXT_CALLOUT_FORMAT_BEFORE, lastDate];
    }
    else
    {
        _lastUpdateDateLabel.text = TEXT_CALLOUT_FORMAT_NON_BEFORE;
    }
    
    //
    _currentUpdateDateLabel.text = [NSString stringWithFormat:TEXT_CALLOUT_FORMAT_NOW, currentDate];
    
    //
    NSString *value = [dict objectForKey:GOLD_BUY_KEY];
    float fValue = [Utility convertStringToFloat:value];
    _currentUpdateBuyLabel.text = value;
    
    float fPreValue = .0f;
    NSString *preValue = [dict objectForKey:PRE_GOLD_BUY_KEY];
    if (preValue == nil || preValue.length == 0)
    {
        _lastUpdateBuyLabel.text = TEXT_CHUA_CO;
    }
    else
    {
        fPreValue = [Utility convertStringToFloat:preValue];
        _lastUpdateBuyLabel.text = preValue;
    }

#ifdef __DUMMY_DATA__
    fPreValue = fValue + .3f;
#endif
    
    UIColor *textColor = COLOR_GREEN_NORMAL;
    
    if (fValue < fPreValue)
    {
        textColor = [UIColor redColor];
    }
    
    if (fPreValue > .0f && fValue != fPreValue)
    {
        changeTextColor = COLOR_BLUE_IOS7;
    }
    
    [_buyChangeLabel setTextColor:textColor];
    [_buyChangeLabel setText:[Utility makeChangedString:fValue
                                               preValue:fPreValue
                                        ignoreZeroValue:NO
                                   changedPerInDiffLine:YES]];
    
    //
    value = [dict objectForKey:GOLD_SELL_KEY];
    fValue = [Utility convertStringToFloat:value];
    _currentUpdateSellLabel.text = value;
    
    preValue = [dict objectForKey:PRE_GOLD_SELL_KEY];
    fPreValue = .0f;
    if (preValue == nil || preValue.length == 0)
    {
        _lastUpdateSellLabel.text = TEXT_CHUA_CO;
    }
    else
    {
        fPreValue = [Utility convertStringToFloat:preValue];
        _lastUpdateSellLabel.text = preValue;
    }
    
#ifdef __DUMMY_DATA__
    fPreValue = fValue - .3f;
#endif
    
    textColor = COLOR_GREEN_NORMAL;
    
    if (fValue < fPreValue)
    {
        textColor = COLOR_RED_NORMAL;
    }
    
    if (fPreValue > .0f && fValue != fPreValue)
    {
        changeTextColor = COLOR_BLUE_IOS7;
    }
    
    [_sellChangeLabel setTextColor:textColor];
    [_sellChangeLabel setText:[Utility makeChangedString:fValue
                                                preValue:fPreValue
                                         ignoreZeroValue:NO
                                    changedPerInDiffLine:YES]];
    
    _changeLabel.text = NSLocalizedString(@"Thay đổi:", nil);
    _changeLabel.textColor = changeTextColor;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if( point.x > 0 && point.x < self.frame.size.width && point.y > 0 && point.y < self.frame.size.height )
    {
        return self;
    }
    
    if ([self checkPointInsideCurrentSelectedIndex:point])
    {
        return [super hitTest:point withEvent:event];
    }
    
    if ([_mainDelegate respondsToSelector:@selector(detailUpDownPriceCalloutViewouchOutSide:atPoint:)])
    {
        [_mainDelegate detailUpDownPriceCalloutViewouchOutSide:self atPoint:point];
    }
    
    return [super hitTest:point withEvent:event];
}

- (BOOL)checkPointInsideCurrentSelectedIndex:(CGPoint)point
{
    CGRect rectSelectedIndexPath = CGRectZero;
    if ([_mainDelegate respondsToSelector:@selector(detailUpDownPriceCalloutViewRectForSelectedIndex:)])
    {
        rectSelectedIndexPath = [_mainDelegate detailUpDownPriceCalloutViewRectForSelectedIndex:self];
    }
    
    point = [[DataSingleton shared].rootViewController.view convertPoint:point fromView:self];
    if (CGRectContainsPoint(rectSelectedIndexPath, point))
    {
        return YES;
    }
    
    return NO;
}

@end
